<template>
  <div style="border: 1px solid #ccc;">
    <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" />
    <Editor
      style="height: 500px; overflow-y: hidden;"
      v-model="html"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
    />
  </div>
</template>
<script>
import { Boot } from '@wangeditor/editor'
import fillInBlank from 'wangeditor-plugin-fillinblank'
import formulaModule from '@wangeditor/plugin-formula'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import Vue from 'vue'
// 注册。要在创建编辑器之前注册，且只能注册一次，不可重复注册。
Boot.registerModule(formulaModule)
Boot.registerModule(fillInBlank)
export default Vue.extend({
  components: { Editor, Toolbar },
  props: {
    htmlTwo: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      editor: null,
      html: '',
      toolbarConfig: {
        insertKeys: {
          index: 0,
          keys: [
            'insertFormula', // “插入公式”菜单
            {
              key: 'group-fill-blank',
              title: '填空空格',
              iconSvg: fillInBlank.icon,
              menuKeys: ['insertFillBlank', 'insertLongFillBlank']
            }
            // 'editFormula' // “编辑公式”菜单
          ]
        }
      },

      editorConfig: {
        placeholder: '请输入内容...',
        MENU_CONF: {
          uploadImage: {
            server: 'http:/localhost:8080/jeecg-boot/rsfile/rsFile/uploadImg', // 上传图片地址
            fieldName: 'file',
            customInsert(res, insertFn) {
              // TS 语法
              // customInsert(res, insertFn) {                  // JS 语法
              // res 即服务端的返回结果

              // 从 res 中找到 url alt href ，然后插图图片
              insertFn(res.result)
            }
          }
        },
        hoverbarKeys: {
          formula: {
            menuKeys: ['editFormula'] // “编辑公式”菜单
          }
        }
      },
      mode: 'default' // or 'simple'
    }
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
      editor.setHtml(this.htmlTwo)
    }
  },
  watch: {
    html(val) {
      this.$emit('contentChange', val, this.editor.getText())
    },
    htmlTwo(val) {
      this.html = val
    }
  },
  mounted() {},
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  }
})
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style>
.w-e-modal {
  left: 30px;
}
</style>
